<?php
/**
 * Created by PhpStorm
 * User: huangjiacheng
 * Date: 2023/6/5
 * Time: 9:43
 */

namespace app\command;


use think\facade\Db;
use think\console\input\Option;
use think\console\Command;
use think\facade\Console;
use think\console\Output;
use think\console\Input;


class UpdateCode extends Command
{
    protected function configure()
    {
        $this->setName('update:code')
            ->setDescription('更新code信息脚本');
    }

    protected function execute(Input $input, Output $output)
    {
        $output->info('这是更新code信息脚本');
        $date  = date("Y-m-d");

        $sql = "select * from base_info where  `code` like '3%' and updateTime<'$date' "; // 仅仅更新3开头数据
        $list = Db::query($sql);
        var_dump($list);
        foreach ($list as $row){
            try {
                $url= 'https://api.mairuiapi.com/hsrl/ssjy/' .$row['code']."/".env('mairui.licence');
                $str=file_get_contents($url);
                $data=json_decode($str,true);
                sleep(3);
                $ltsz= $data['lt'];
                $sz= $data['sz'];
                $lb = $data['lb'];
                $hs = $data['hs'];
                $date = date("Y-m-d H:i:s");
                $insert="replace into base_info(`code`, `sz`,`ltsz`,`addTime`,`updateTime`) values('".$row['code']."','$sz','$ltsz','$date','$date')";
                Db::query($insert);
            }  catch (\Exception $e) {
                echo $e;
            }

        }
           $output->info('end');
    }
}